1 简单的邮件发送
 #!/usr/bin/python
import smtplib
import time
from email.mime.text import MIMEText

mail_content = 'test'
mail_from = 'test@126.com'
mail_to = ['test@qq.com']

msg=MIMEText(mail_content)
msg['Subject']='test mail ok'
msg['From']=mail_from
msg['To']=';'.join(mail_to)

smtp=smtplib.SMTP()
smtp.connect('smtp.126.com')
smtp.login('username','password')
smtp.sendmail(mail_from,mail_to,msg.as_string())
smtp.quit()
print 'ok'
2 添加附件发送邮件
[root@centos python]# cat aaa.py 
#!/usr/bin/python
# -*- coding: cp936 -*-

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
 
mail_content='hello this is a test mail,please check it'
mail_from = 'cncyo01@126.com'
mail_to = ['511589735@qq.com']

msg=MIMEMultipart(mail_content)
msg['Subject']='测试'
msg['From']=mail_from
msg['To']=';'.join(mail_to)
body=MIMEText(mail_content)

att=MIMEText(open('/data/python/1.gif','rb').read(),'base64','gb2312')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'filename="1.gif"'
msg.attach(att)
msg.attach(body)
 
smtp=smtplib.SMTP()
smtp.connect('smtp.126.com')
smtp.login('cncyo01','cnxy2012')
smtp.sendmail(mail_from,mail_to,msg.as_string())
smtp.quit()

print 'ok'